<?php
namespace app\logic;

use app\models\Goods;
use app\models\GoodsSku;
use app\models\Partner;
use app\models\ServiceSku;
use app\models\User;
use yii\db\Query;


class UserLogic
{


    public function getUser($query = [], $page = 1, $limit = 10)
    {
        $user = User::find()->filterWhere($query)->orderBy('user_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $user->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $user->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'user/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function addUser($params){
        if(User::findOne(['phone'=>$params['phone']])){
            return false;
        }else{
            $user = new User();
            $user->phone = $params['phone'];
            $user->type = $params['type'];
            $user->create_time = date('Y-m-d H:i:s');
            $user->status = 0;
            return $user->save();
        }
    }

    public function editUserById($id, $params)
    {
        if(!$user = User::findOne($id))
            return false;
        $check = User::find()->where("user_id!=$id AND phone='".$params['phone']."'")->one();
        if($check)
            return ['result'=>false,'msg'=>'该手机号已存在'];
        $shop = ServiceSku::findOne(['owner_user_id'=>$id]);
        if($shop && ($params['type']==0 || $params['type']==2))
            return ['result'=>false,'msg'=>'该用户已绑定为店主，请先解除店主合同'];
        $manager = Goods::findOne(['user_id'=>$id]);
        if($manager && ($params['type']==0 || $params['type']==1))
            return ['result'=>false,'msg'=>'该用户已绑定为店长，请先解除店长合同'];
        $user->phone = $params['phone'];
        $user->type = $params['type'];
        if($user->save()){
            return ['result'=>true];
        }
    }

    public function saveStatus($id)
    {
        $user = User::findOne($id);
        if($user->status)
            $user->status = 0;
        else
            $user->status = 1;
        return $user->save();
    }


    public function getById($user_id){
        return User::findOne($user_id)->toArray();
    }

    public function getAllPartner($query = [], $page = 1, $limit = 10){
        $partner = Partner::find()
            ->filterWhere($query)
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $partner->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $partner->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'partner/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function addPartner($params){
        $partner = new Partner();
        $partner_no = $this->createOrderNo();
        $partner_temp = Partner::find()->where(['partner_no'=>$partner_no])->asArray()->one();
        while($partner_temp){
            $partner_no = $this->createOrderNo();
            $partner_temp = Partner::find()->where(['partner_no'=>$partner_no])->asArray()->one();
        }
        $partner->partner_no = $partner_no;
        $partner->partner_name = $params['partner_name'];
        $partner->linkman = $params['linkman'];
        $partner->linkphone = $params['linkphone'];
        $partner->linkemail = $params['linkemail'];
        $partner->partner_remark = $params['partner_remark'];
        $partner->ftp_username = $params['ftp_username'];
        $partner->ftp_password = $params['ftp_password'];
        return $partner->save();
    }

    public function createOrderNo(){
        return mt_rand(100000,999999);
        /*$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        return $yCode[intval(date('Y')) - 2018] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));*/
    }

    public function getPartnerById($id)
    {
        return Partner::find()->where(['partner_id' => $id])->asArray()->one();
    }

    public function editPartnerById($id, $params)
    {
        if(!$partner = Partner::findOne($id)) return false;
        $partner->partner_no = $params['partner_no'];
        $partner->partner_name = $params['partner_name'];
        $partner->linkman = $params['linkman'];
        $partner->linkphone = $params['linkphone'];
        $partner->linkemail = $params['linkemail'];
        $partner->partner_remark = $params['partner_remark'];
        $partner->ftp_username = $params['ftp_username'];
        if(isset($params['ftp_password']) && $params['ftp_password'])
            $partner->ftp_password = $params['ftp_password'];
        return $partner->save();
    }

    public function savePartnerStatus($id)
    {
        $partner = Partner::findOne($id);
        if($partner->partner_status)
            $partner->partner_status = 0;
        else
            $partner->partner_status = 1;
        return $partner->save();
    }

    public function getPartners(){
        return Partner::find()->where(['partner_status'=>1])->asArray()->all();
    }

}